package com.uphone.kingmall.activity.personal;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.GoodsDetailActivity;
import com.uphone.kingmall.adapter.FavoriteAdapter;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseListActivity;
import com.uphone.kingmall.bean.UserCollectGoodsBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.HttpParamsUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PopupUtil;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;

/**
 * <pre>
 *     author : 王雨
 *     e-mail :
 *     time   : 2018/07/31
 *     desc   :收藏夹
 *     version: 1.0
 * </pre>
 */
public class FavoritesActivity extends BaseListActivity<UserCollectGoodsBean, UserCollectGoodsBean.DataBean> {
    private EditText etKeyWord;
    private FavoriteAdapter adapter;
    private PopupUtil popupFilter;
    private PopupUtil searchPopup;
    private CheckBox iv_select;

    @Override
    protected String initTitle() {
        return "收藏夹";
    }

    @Override
    protected void setTitle(final DefaultNavigationBar navigationBar) {
        super.setTitle(navigationBar);
        navigationBar.setRightTitle("管理");
        navigationBar.setRightIcon(R.mipmap.search_poi);
        navigationBar.setIvRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupFilter != null) {
                    popupFilter.dismiss();
                    navigationBar.setRightTitle("管理");
                    adapter.setManager(false);
                }
                // 搜索
                CommonUtil.startIntent(FavoritesActivity.this,FavoritesSearchActivity.class);
//                searchPopup();
            }
        });
        navigationBar.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    String text = tv.getText().toString();
                    if (!TextUtils.isEmpty(text) && text.contains("管理")) {
                        // 变成删除
                        tv.setText("完成");
                        adapter.setManager(true);
                        showPopup();
                    } else {
                        tv.setText("管理");
                        adapter.setManager(false);
                        if (popupFilter != null) {
                            popupFilter.dismiss();
                        }
                    }
                }
            }
        });
    }


    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {
        CommonUtil.startIntent(this, GoodsDetailActivity.class, dataList.get(position).getGoodsId());
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        adapter = new FavoriteAdapter();
        adapter.setOnShopItemClickListener(new FavoriteAdapter.OnShopItemClickListener() {
            @Override
            public void itemSelect(View v, int position, UserCollectGoodsBean.DataBean item) {

                itemSelectChange(position, item);
            }
        });
        return adapter;
    }

    @Override
    protected UserCollectGoodsBean getBeans() {
        return new UserCollectGoodsBean();
    }

    @Override
    protected HttpParams getParams() {
        HttpParams params = HttpParamsUtils.getInstance().put("page", page)
                .put("limit", 10).getParams();
        if (!CommonUtil.checkViewEmpty(etKeyWord)) {
            params.put("keyword", etKeyWord.getText().toString().trim());
        }
        return params;
    }

    @Override
    protected String getUrl() {
        return MyUrl.getUserCollectGoods;
    }

    private void showPopup() {
        View inflate = View.inflate(this, R.layout.kingmall_popup_history_delete, null);
        popupFilter = PopupUtil.getInstance(MyApplication.mContext, inflate);
        popupFilter.setOutsideTouchable(false).setFocusable(false).showBottom();
        popupFilter.setDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (popupFilter != null) {
                    popupFilter.release();
                }
            }
        });

        iv_select = inflate.findViewById(R.id.ivSelectAll);
        TextView tv_delete = inflate.findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
        iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = iv_select.isChecked();
                for (int i = 0; i < dataList.size(); i++) {
                    dataList.get(i).setSelect(isChecked);
                }
                adapter.setNewData(dataList);
            }
        });
    }

    private void searchPopup() {
        View inflate = View.inflate(this, R.layout.kingmall_popup_search, null);
        searchPopup = PopupUtil.getInstance(MyApplication.mContext, inflate);
        searchPopup.setBasisView(recyclerview).setOutsideTouchable(false).setFocusable(true).showTop();
        searchPopup.setDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (searchPopup != null) {
                    searchPopup.release();
                }
            }
        });
        inflate.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchPopup != null) {
                    searchPopup.dismiss();
                }
                if (etKeyWord != null) {
                    etKeyWord.setText("");
                    etKeyWord.setHint("请输入您的搜索内容");
                }
            }
        });
        etKeyWord = inflate.findViewById(R.id.et_content);
        etKeyWord.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyboardUtils.showSoftInput(v);
                return true;
            }
        });
        etKeyWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                loadData(1);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupFilter != null) {
            popupFilter.release();
        }
        if (searchPopup != null) {
            searchPopup.release();
        }
    }

    private void delete() {
        String shopCarId = "";
        if (dataList != null && dataList != null && dataList.size() > 0) {
            for (int i = 0; i < dataList.size(); i++) {
                UserCollectGoodsBean.DataBean goodsBean = dataList.get(i);
                if (goodsBean.isSelect()) {
                    shopCarId += goodsBean.getFavId() + ",";
                }
            }
        }
        if (TextUtils.isEmpty(shopCarId)) {
            ToastUtils.showShortToast(this, "您还没有选择商品");
            return;
        }
        String str = shopCarId.substring(0, shopCarId.length() - 1);
        HttpParams params = new HttpParams();
        params.put("favIds", str);

        OkGoUtils.progressRequest(MyUrl.delUserFavorite, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    // 刷新
                    loadData(1);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private void itemSelectChange(int position, UserCollectGoodsBean.DataBean item) {
        dataList.set(position, item);
        refresh();
    }

    private void refresh() {
        if (popupFilter == null) {
            return;
        }
        if (iv_select == null) {
            return;
        }
        boolean check = true;
        for (int i = 0; i < dataList.size(); i++) {
            if (!dataList.get(i).isSelect()) {
                check = false;
                break;
            }
        }
        iv_select.setChecked(check);
    }
}
